<?php

/*
 * ******************************************
 * SET DEBUG LEVEL
 * ******************************************
 */

if (DEBUG) {
	ini_set ( 'display_errors', true ); // INI SET
	error_reporting ( E_ALL );
} else {
	ini_set ( 'display_errors', false ); // INI SET
	error_reporting ( 0 );
}

/*
 * *******************************************
 * Start PHP Cache Initialize with Gzip
 * *******************************************
 */
if (extension_loaded ( 'zlib' )) {
	ob_start ( 'ob_gzhandler' );
} else {
	ob_start ();
}
/*
 * ************************************************************************
 * Set default cache flush to false to prevent autmatic flush of the cache
 * *************************************************************************
 */
ob_implicit_flush ( false ); // 


/*
 * ********************************************
 * DEFINE APP_PATH
 * ********************************************
 */

define ( 'APP_PATH', dirname ( __FILE__ ) . DS );

/*
 * ********************************************
 * Include and load Configuration file
 * ********************************************
 */
include APP_PATH . 'config.php';

/*
 * *********************************************
 * Include General Functions
 * *********************************************
 */

include 'functions.php';

/*
 * *******************************************
 * SET INCLUDE PATH
 * *******************************************
 */

set_include_path ( lib_path () . PATH_SEPARATOR . get_include_path () );

/*
 * **********************************************
 * Unregister Globals to prevent Globals Hack
 * **********************************************
 */
unregister_globals ();

/*
 * ***********************************************
 * Register Autoloader
 * ***********************************************
 */
spl_autoload_register ( 'autoload' );

/*
 * *************************************************
 * Start the Session
 * *************************************************
 */

Session::newInstance ()->session_start ();

/*
 * ****************************************************
 * Run Rewrite to match rewrited URI's to normal urls
 * ****************************************************
 */

$db = DB::newInstance ()->con;
Zend_Registry::set ( 'db', $db );
Zend_Db_Table_Abstract::setDefaultAdapter ( $db );
Rewrite::newInstance ()->init ();

/*
 * *******************************************************
 * Determine whether it is admin request or normal request
 * *******************************************************
 */

if (Params::getParam ( 'CP' ) == ADMIN) {
	include cp_path () . 'application.php';
} else {
	
	$page = Params::getParam ( 'page' );
	
	/*
	 * ***********************************************************************
	 * NOW RUN FRONTEND
	 * ***********************************************************************
	 */
	
	/*
	 * *****************************************************************
	 * Load Top Locations and Top Categories Globally availalble
	 * *****************************************************************
	 */
	
	View::newInstance ()->_exportVariableToView ( 'tree', Model_Mapper_Categories::newInstance ()->tree () );
	View::newInstance ()->_exportVariableToView ( 'top_categories', Model_Mapper_Categories::newInstance ()->getTopCategories () );
	View::newInstance ()->_exportVariableToView ( 'main_categories', Model_Mapper_Categories::newInstance ()->tree () );
	View::newInstance ()->_exportVariableToView ( 'top_locations', Model_Mapper_Locations::newInstance ()->getTopLocations () );
	
	switch ($page) {
		case 'item' :
			include code_path () . 'item.php';
			$app = new Application ();
			$app->run ();
			break;
		
		case 'category' :
			include code_path () . 'category.php';
			$app = new Application ();
			$app->run ();
			break;
		
		case 'location' :
			/*
			include code_path () . 'location.php';
			$app = new Application ();
			$app->run ();
			$cat = new Helper_Categories ();
			*/
			include (code_path () . 'home.php');
			$app = new Application ();
			$app->run ();
			break;
		
		case 'user' :
			if (! params::getParam ( 'action' )) {
				params::setParam ( 'action', 'dashboard' );
			}
			if (! user_logged_in () && 'sign-in' != params::getParam ( 'action' ) && 'register' != params::getParam ( 'action' )) {
				redirect ( 'user/sign-in/' );
			}
			include code_path () . 'user.php';
			$app = new Application ();
			$app->run ();
			break;
		
		case 'search' :
			include code_path () . 'search.php';
			$app = new Application ();
			$app->run ();
			break;
		
		case 'ajax' :
			include code_path () . 'ajax.php';
			$app = new Application ();
			$app->run ();
			break;
		case 'tag' :
			include code_path () . 'tag.php';
			$app = new Application ();
			$app->run ();
			break;
		
		case '404' :
			include code_path () . '404.php';
			$app = new Application ();
			$app->run ();
			break;
		case 'unit' :
			$user = Model_Mapper_User::newInstance ()->getByEmail ( 'sapan.myhighst@gmail.com' );
			$user->sendActivationEmail ();
			$user = Model_Mapper_User::newInstance ()->getByEmail ( 'developer.roshan@gmail.com' );
			$user->sendActivationEmail ();
			break;
		case 'home' :
			$cat = new Helper_Categories ();
			include (code_path () . 'home.php');
			$app = new Application ();
			$app->run ();
			break;
		default :
			include code_path () . '404.php';
			$app = new Application ();
			$app->run ();
			break;
	
	}

}